package com.simp.process;

import lombok.extern.slf4j.Slf4j;
import org.springframework.core.io.ClassPathResource;

import java.io.*;

/**
 * @author pan.shun
 * @since 2023/7/27 22:52
 */
@Slf4j
public class RunProcess {
    /**
     * 项目初始化shell运行脚本生成
     */
    protected static void initShellWords(String jarName) {
        String absolutePath = new File("").getAbsolutePath();
        File[] files = new File(absolutePath).listFiles();
        if (null != files && files.length != 0) {
            int flag = 0;
            for (File f : files) {
                String fileName = f.getName();
                if ("restart.sh".equals(fileName)) {
                    flag = 1;
                }
            }
            if (flag == 0) {
                FileWriter fileWriter = null;
                try {
                    log.info("当前目录暂无restart.sh 文件，开始初始化生成restart.sh 脚本文件");
                    fileWriter = new FileWriter(absolutePath + File.separator + "restart.sh");
                    fileWriter.write("#!/bin/bash\r\n");
                    fileWriter.write("projectName='" + jarName + "'\r\n\r\n");
                    fileWriter.write("logName='run.log'\r\n");
                    fileWriter.write("pids=$(ps -ef | grep ${projectName}  | grep -v gre | awk '{print $2}')\r\n");
                    fileWriter.write("if [ $pids ];\r\n");
                    fileWriter.write("then\r\n");
                    fileWriter.write("echo '当前服务处于启动中，进程号为' $pids\r\n\r\n");
                    fileWriter.write("kill -9 $pids\r\n");
                    fileWriter.write("nohup java -jar $projectName >$logName &\r\n");
                    fileWriter.write("tail -f $logName\r\n");
                    fileWriter.write("else\r\n");
                    fileWriter.write(" echo '当前服务暂未启动，开始初始化启动'\r\n");
                    fileWriter.write(" nohup java -jar $projectName >$logName &\r\n");
                    fileWriter.write(" tail -f $logName\r\n");
                    fileWriter.write("fi\r\n");
                } catch (IOException e) {
                    log.error("初始化生成restart.sh 脚本文件失败，异常信息 {}", e.getMessage());
                } finally {
                    try {
                        if (null != fileWriter) {
                            fileWriter.close();
                        }
                    } catch (IOException e) {
                        log.error("流关闭异常，{}", e);
                    }
                }
            }
        }
    }

    protected static boolean check() {
        String absolutePath = new File("").getAbsolutePath();
        File file = new File(absolutePath);
        File[] files = file.listFiles();
        if (null != files) {
            for (File f : files) {
                if (!f.getName().contains("yml") && !f.getName().contains("sh")) {
                    return true;
                }
            }
        }
        return false;
    }

    /**
     * 初始化配置文件
     */
    protected static void createYml() {
        createYml("application.yml");
        createYml("application-prod.yml");
    }

    private static void createYml(String fileName) {
        ClassPathResource classPathResource = new ClassPathResource(fileName);
        BufferedInputStream bis = null;
        BufferedOutputStream bos = null;
        try {
            InputStream inputStream = classPathResource.getInputStream();

            File file = new File(new File("").getAbsolutePath() + File.separator + fileName);
            bis = new BufferedInputStream(inputStream);
            bos = new BufferedOutputStream(new FileOutputStream(file));
            byte[] b = new byte[1024];
            int len;
            while (-1 != (len = bis.read(b, 0, b.length))) {
                bos.write(b, 0, len);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (null != bos) {
                try {
                    bos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (null != bis) {
                try {
                    bis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
